package org.beian.fmmall.service.Impl;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.beian.fmmall.VO.ResStatus;
import org.beian.fmmall.VO.ResultVO;
import org.beian.fmmall.beans.Users;
import org.beian.fmmall.dao.UsersMapper;
import org.beian.fmmall.service.UserService;
import org.beian.fmmall.utils.SHA256Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    private ObjectMapper objectMapper=new ObjectMapper();

    @Override
    public ResultVO userRegist(String name, String pwd) {
        if(name==null||pwd==null){
            return new ResultVO(ResStatus.NO,"注册失败",null);
        }
        Example example = new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username",name);
        List<Users> users = usersMapper.selectByExample(example);
        if(users.size()==0){
            String sha256Pwd = SHA256Utils.sha256(pwd);
            Users user=new Users();
            user.setUsername(name);
            user.setPassword(sha256Pwd);
            user.setUserRegtime(new Date());
            user.setUserModtime(new Date());
            user.setUserImg("img/default.jpg");
            int i = usersMapper.insert(user);
            if(i>0){
                return new ResultVO(ResStatus.OK,"注册成功",null);
            }else{
                return new ResultVO(ResStatus.NO,"注册失败",null);
            }
        }else{
            return new ResultVO(ResStatus.NO,"用户名存在",null);
        }
    }

    @Override
    public ResultVO checkLogin(String name, String pwd) {
        try {
            Example example = new Example(Users.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("username",name);
            List<Users> users = usersMapper.selectByExample(example);
            if(users.size()==0){
                return new ResultVO(ResStatus.NO,"用户名或密码错误！",null);
            }else{
                String shaPwd = SHA256Utils.sha256(pwd);
                if(users.get(0).getPassword().equals(shaPwd)){
                    JwtBuilder builder = Jwts.builder();
                    String token = builder.setSubject(name)
                            .setId(users.get(0).getUserId() + "")
                            .setIssuedAt(new Date())
                            .setExpiration(new Date(System.currentTimeMillis() + 60 * 60 * 1000))
                            .signWith(SignatureAlgorithm.HS256, "beian")
                            .compact();
                    String s = objectMapper.writeValueAsString(users.get(0));
                    stringRedisTemplate.boundValueOps(token).set(s,30, TimeUnit.MINUTES);
                    return new ResultVO(ResStatus.OK,token,users.get(0));
                }else{
                    return new ResultVO(ResStatus.NO,"用户名或密码错误！",null);
                }
            }
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
